﻿using LH.Model.PU;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using fuzhu;
using LH.Model.ST;

namespace LH.U8API.Bus
{
   public static class GetInfo
    {
        public static string GetPUArrivalInfo(Pu_ArrivalVouch pu_ArrivalVouch,string cConstr)
        {
            if (pu_ArrivalVouch == null)
            {
                return "Error: 没有数据";
            }
            if (pu_ArrivalVouch.bodies == null)
            {
                return "Error: 没有表体数据";
            }
         //var listSummary = from p in pu_ArrivalVouch.bodies
         //                              group p by new { iposid = p.iposid } into g
         //                     select new
         //                     {
         //                         iposid = g.Key.iposid,
         //                         iquantity = g.Sum(a =>DbHelper.GetDbdecimal(a.iquantity))
         //                     } ;
            //超采购订单到货的校验
            //foreach(var lstgroup in listSummary)
            //{
            //    string cPosId = lstgroup.iposid;
            //    decimal dQty = lstgroup.iquantity;
            //    string sql= "SELECT COUNT(*)  FROM dbo.PO_Podetails WHERE id='{0}' AND iQuantity<ISNULL(iArrQTY,0)+{0} "

            //}
            for (int i = 0; i < pu_ArrivalVouch.bodies.Count; i++)
            {
                
                string cPosId = pu_ArrivalVouch.bodies[i].iposid;
                string cInvCode= pu_ArrivalVouch.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cPOID,po.cDepCode,po.cPersonCode,po.cVenCode,po.cPTCode,po.cexch_name,po.iTaxRate,po.nflat,po.cBusType, a.bGsp,a.bTaxCost,a.iPerTaxRate, a.iTaxPrice,
b.bInvQuality
FROM dbo.PO_Podetails a
   INNER JOIN dbo.PO_Pomain po ON a.POID=po.POID
  INNER JOIN dbo.Inventory b  ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.cUnitID=c.cComunitCode
  WHERE a.id = '{0}' ", cPosId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行订单ID不正确，查询不到订单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != pu_ArrivalVouch.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        pu_ArrivalVouch.head.cptcode = DbHelper.GetDbString(dt.Rows[0]["cPTCode"]);
                        pu_ArrivalVouch.head.cvencode = DbHelper.GetDbString(dt.Rows[0]["cVenCode"]);
                        pu_ArrivalVouch.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                        pu_ArrivalVouch.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        pu_ArrivalVouch.head.cpocode = DbHelper.GetDbString(dt.Rows[0]["cPOID"]);
                        pu_ArrivalVouch.head.cexch_name = DbHelper.GetDbString(dt.Rows[0]["cexch_name"]);
                        pu_ArrivalVouch.head.itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                        pu_ArrivalVouch.head.iexchrate = DbHelper.GetDbString(dt.Rows[0]["nflat"]);
                        pu_ArrivalVouch.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]);

                    }
                    pu_ArrivalVouch.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    pu_ArrivalVouch.bodies[i].bgsp = DbHelper.GetDbString(dt.Rows[0]["bGsp"]);
                    pu_ArrivalVouch.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    pu_ArrivalVouch.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iPerTaxRate"]);
                    pu_ArrivalVouch.bodies[i].cgroupcode = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                    pu_ArrivalVouch.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["iTaxPrice"]);


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cUnitID"])))
                    {
                        pu_ArrivalVouch.bodies[i].cunitid = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                        pu_ArrivalVouch.bodies[i].igrouptype = "1";
                    }
                    else
                    {
                        pu_ArrivalVouch.bodies[i].igrouptype = "0";
                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        pu_ArrivalVouch.bodies[i].cunitid = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }



                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        pu_ArrivalVouch.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);
                    }
                    if (!string.IsNullOrEmpty(pu_ArrivalVouch.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            pu_ArrivalVouch.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            pu_ArrivalVouch.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(pu_ArrivalVouch.bodies[i].dpdate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(pu_ArrivalVouch.bodies[i].dpdate);
                            //1 年 2 月 日
                            if (pu_ArrivalVouch.bodies[i].cmassunit == "1")
                            {
                                pu_ArrivalVouch.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (pu_ArrivalVouch.bodies[i].cmassunit == "2")
                            {
                                pu_ArrivalVouch.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (pu_ArrivalVouch.bodies[i].cmassunit == "3")
                            {
                                pu_ArrivalVouch.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            pu_ArrivalVouch.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                pu_ArrivalVouch.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(pu_ArrivalVouch.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                pu_ArrivalVouch.bodies[i].dexpirationdate = DbHelper.GetDbDate(pu_ArrivalVouch.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
             }
            return "ok";
        }


        public static string GetST01ByCheckInfo(Rdrecord01 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {

                string cCheckId = rdrecord.bodies[i].icheckidbaks;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cCode,qm.CPOCODE,po.cDepCode,po.cPersonCode,po.cVenCode,po.cPTCode,po.cexch_name, po.iExchRate,po.cBusType, a.bGsp,a.bTaxCost,a.iTaxRate, a.ioritaxcost
,po.dDate,qm.CCHECKCODE,a.iposid,a.autoid,qm.CCHECKPERSONCODE
FROM dbo.QMCHECKVOUCHER qm 
INNER JOIN dbo.PU_ArrivalVouchs a ON qm.SOURCEAUTOID = a.Autoid 
   INNER JOIN dbo.PU_ArrivalVouch po ON a.id=po.id
  INNER JOIN dbo.Inventory b  ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.cUnitID=c.cComunitCode
  WHERE qm.CVOUCHTYPE='QM03' AND  qm.id = '{0}' ", cCheckId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行检验单ID不正确，查询不到检验单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        rdrecord.head.cptcode = DbHelper.GetDbString(dt.Rows[0]["cPTCode"]);
                        rdrecord.head.cvencode = DbHelper.GetDbString(dt.Rows[0]["cVenCode"]);
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                        rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        rdrecord.head.cordercode = DbHelper.GetDbString(dt.Rows[0]["CPOCODE"]);
                        rdrecord.head.carvcode = DbHelper.GetDbString(dt.Rows[0]["cCode"]);
                        rdrecord.head.cexch_name = DbHelper.GetDbString(dt.Rows[0]["cexch_name"]);
                        rdrecord.head.itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                        rdrecord.head.iexchrate = DbHelper.GetDbString(dt.Rows[0]["iExchRate"]);
                        rdrecord.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]);
                        rdrecord.head.darvdate = DbHelper.GetDbString(dt.Rows[0]["dDate"]);
                        rdrecord.head.cchkcode = DbHelper.GetDbString(dt.Rows[0]["CCHECKCODE"]);

                    }
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    rdrecord.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    rdrecord.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                    //rdrecord.bodies[i]. = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                    rdrecord.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["ioritaxcost"]);
                    rdrecord.bodies[i].iposid = DbHelper.GetDbString(dt.Rows[0]["iposid"]);
                    rdrecord.bodies[i].iarrsid = DbHelper.GetDbString(dt.Rows[0]["autoid"]);
                    rdrecord.bodies[i].ccheckcode = DbHelper.GetDbString(dt.Rows[0]["CCHECKCODE"]);
                    rdrecord.bodies[i].ccheckpersoncode = DbHelper.GetDbString(dt.Rows[0]["CCHECKPERSONCODE"]);
                    rdrecord.bodies[i].dcheckdate = DbHelper.GetDbString(dt.Rows[0]["autoid"]);
                    rdrecord.bodies[i].cpoid = DbHelper.GetDbString(dt.Rows[0]["CPOCODE"]);
                    rdrecord.bodies[i].cbarvcode = DbHelper.GetDbString(dt.Rows[0]["cCode"]);
                    rdrecord.bodies[i].dbarvdate = DbHelper.GetDbString(dt.Rows[0]["dDate"]);

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cUnitID"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cUnitID"]);
                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }

        public static string GetST01ByArrInfo(Rdrecord01 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {

                string cArrsId = rdrecord.bodies[i].iarrsid;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cCode ,po.cDepCode,po.cPersonCode,po.cVenCode,po.cPTCode,po.cexch_name, po.iExchRate,po.cBusType, a.bGsp,a.bTaxCost,a.iTaxRate, a.ioritaxcost
,po.dDate, a.iposid,a.autoid  ,a.cordercode as cpocode
FROM   dbo.PU_ArrivalVouchs a  
   INNER JOIN dbo.PU_ArrivalVouch po ON a.id=po.id 
  INNER JOIN dbo.Inventory b  ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.cUnitID=c.cComunitCode
  WHERE a.autoid  = '{0}' ", cArrsId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行到货单ID不正确，查询不到到货单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        rdrecord.head.cptcode = DbHelper.GetDbString(dt.Rows[0]["cPTCode"]);
                        rdrecord.head.cvencode = DbHelper.GetDbString(dt.Rows[0]["cVenCode"]);
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                        rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        rdrecord.head.cordercode = DbHelper.GetDbString(dt.Rows[0]["cpocode"]);
                        rdrecord.head.carvcode = DbHelper.GetDbString(dt.Rows[0]["cCode"]);
                        rdrecord.head.cexch_name = DbHelper.GetDbString(dt.Rows[0]["cexch_name"]);
                        rdrecord.head.itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                        rdrecord.head.iexchrate = DbHelper.GetDbString(dt.Rows[0]["iExchRate"]);
                        rdrecord.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]);
                        rdrecord.head.darvdate = DbHelper.GetDbString(dt.Rows[0]["dDate"]);
                        //rdrecord01.head.cchkcode = DbHelper.GetDbString(dt.Rows[0]["CCHECKCODE"]);

                    }
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    rdrecord.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    rdrecord.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                    //rdrecord.bodies[i]. = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                   
                    if (rdrecord.head.cbustype == "普通采购")
                    {
                        rdrecord.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["ioritaxcost"]);
                        //写其他金额
                        decimal dSum = Math.Round(DbHelper.GetDbdecimal(dt.Rows[0]["ioritaxcost"]) * DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity), 2);
                    decimal dMoney = Math.Round(dSum / (1 + DbHelper.GetDbdecimal(dt.Rows[0]["iTaxRate"]) / 100), 2);
                    decimal dTax = dSum - dMoney;
                        decimal ioricost = 0.00m;
                        if (DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity)!=0)
                        {
                            ioricost = dMoney / DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity);

                        }
                 
                    decimal dExch = DbHelper.GetDbdecimal(dt.Rows[0]["iExchRate"]);
                    
                        rdrecord.bodies[i].iorisum = dSum.ToString("0.00");
                        rdrecord.bodies[i].iorimoney = dMoney.ToString("0.00");
                        rdrecord.bodies[i].ioritaxprice = (dSum - dMoney).ToString("0.00");
                        rdrecord.bodies[i].iprice = (dMoney * dExch).ToString("0.00");
                        rdrecord.bodies[i].iaprice = (dMoney * dExch).ToString("0.00");
                        rdrecord.bodies[i].facost = (ioricost * dExch).ToString("0.000000");

                        rdrecord.bodies[i].isum = (dSum * dExch).ToString("0.00");
                        //rdrecord.bodies[i].imoney = (dMoney * dExch).ToString("0.00");  //累计结算金额，不能写
                        rdrecord.bodies[i].itaxprice = ((dSum - dMoney) * dExch).ToString("0.00");
                        rdrecord.bodies[i].ioricost = ioricost.ToString("0.000.000000");

                        rdrecord.bodies[i].iunitcost = (ioricost * dExch).ToString("0.000000");
                        //rdrecord.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["ioritaxcost"]);
                    }

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    rdrecord.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    rdrecord.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                    //rdrecord01.bodies[i]. = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                  
                    if (rdrecord.head.cbustype == "普通采购")
                    {
                        rdrecord.bodies[i].iposid = DbHelper.GetDbString(dt.Rows[0]["iposid"]);
                    }
                    else
                    {
                        rdrecord.bodies[i].iomodid = DbHelper.GetDbString(dt.Rows[0]["iposid"]);
                    }
                    rdrecord.bodies[i].iarrsid = DbHelper.GetDbString(dt.Rows[0]["autoid"]);
                    //rdrecord01.bodies[i].ccheckcode = DbHelper.GetDbString(dt.Rows[0]["CCHECKCODE"]);
                    //rdrecord01.bodies[i].ccheckpersoncode = DbHelper.GetDbString(dt.Rows[0]["CCHECKPERSONCODE"]);
                    rdrecord.bodies[i].dcheckdate = DbHelper.GetDbString(dt.Rows[0]["autoid"]);
                    rdrecord.bodies[i].cpoid = DbHelper.GetDbString(dt.Rows[0]["cpocode"]);
                    rdrecord.bodies[i].cbarvcode = DbHelper.GetDbString(dt.Rows[0]["cCode"]);
                    rdrecord.bodies[i].dbarvdate = DbHelper.GetDbString(dt.Rows[0]["dDate"]);

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])) && DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"])!=0.00m)
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);
                        rdrecord.bodies[i].inum = (DbHelper.GetDbdecimal(rdrecord.bodies[i].iquantity) / DbHelper.GetDbdecimal(dt.Rows[0]["iChangRate"])).ToString("0.000");
                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }



        public static string GetST01ByPOInfo(Rdrecord01 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {
                string cPosId = rdrecord.bodies[i].iposid;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cPOID,po.cDepCode,po.cPersonCode,po.cVenCode,po.cPTCode,po.cexch_name,po.iTaxRate,po.nflat,po.cBusType, a.bGsp,a.bTaxCost,a.iPerTaxRate, a.iTaxPrice
FROM dbo.PO_Podetails a
   INNER JOIN dbo.PO_Pomain po ON a.POID=po.POID
  INNER JOIN dbo.Inventory b  ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.cUnitID=c.cComunitCode
  WHERE a.id = '{0}' ", cPosId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行采购订单ID不正确，查询不到采购订单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        rdrecord.head.cptcode = DbHelper.GetDbString(dt.Rows[0]["cPTCode"]);
                        rdrecord.head.cvencode = DbHelper.GetDbString(dt.Rows[0]["cVenCode"]);
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                        rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        rdrecord.head.cordercode = DbHelper.GetDbString(dt.Rows[0]["cPOID"]);
                        rdrecord.head.cexch_name = DbHelper.GetDbString(dt.Rows[0]["cexch_name"]);
                        rdrecord.head.itaxrate = DbHelper.GetDbString(dt.Rows[0]["iTaxRate"]);
                        rdrecord.head.iexchrate = DbHelper.GetDbString(dt.Rows[0]["nflat"]);
                        rdrecord.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]);

                    }
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                    rdrecord.bodies[i].btaxcost = DbHelper.GetDbString(dt.Rows[0]["bTaxCost"]);
                    rdrecord.bodies[i].itaxrate = DbHelper.GetDbString(dt.Rows[0]["iPerTaxRate"]);
                    //rdrecord.bodies[i]. = DbHelper.GetDbString(dt.Rows[0]["cGroupCode"]);
                    rdrecord.bodies[i].ioritaxcost = DbHelper.GetDbString(dt.Rows[0]["iTaxPrice"]);
                    //rdrecord.bodies[i].iposid = DbHelper.GetDbString(dt.Rows[0]["iposid"]);
                    rdrecord.bodies[i].cpoid = DbHelper.GetDbString(dt.Rows[0]["cPOID"]);

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }


        public static string GetST10ByMOInfo(Rdrecord10 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {

                string cModid = rdrecord.bodies[i].impoids;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.InvCode,b.cComUnitCode,b.cGroupCode,a.AuxUnitCode,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
 mo.MoCode,a.MDeptCode,a.SoCode,a.SoType,a.MoLotCode  ,a.SortSeq
FROM dbo.mom_orderdetail a 
INNER JOIN mom_order mo ON a.MoId=mo.MoId
  INNER JOIN dbo.Inventory b  ON a.InvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.AuxUnitCode=c.cComunitCode
  WHERE a.modid = '{0}' ", cModid);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行生产订单ID不正确，查询不到生产订单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["InvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        
                     
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["MDeptCode"]);
                       
                        rdrecord.head.cmpocode = DbHelper.GetDbString(dt.Rows[0]["MoCode"]);
                        rdrecord.head.iproorderid = cModid;

                    }
                    //rdrecord10.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]); 
                    rdrecord.bodies[i].cmocode = DbHelper.GetDbString(dt.Rows[0]["MoCode"]);
                    rdrecord.bodies[i].imoseq = DbHelper.GetDbString(dt.Rows[0]["SortSeq"]);
                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);
                        
                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }



        public static string GetST32ByDispInfo(Rdrecord32 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {

                string cdlsId = rdrecord.bodies[i].idlsid;
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT a.cInvCode,b.cComUnitCode,b.cGroupCode,a.cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu,
  po.cDLCode ,po.cDepCode,po.cPersonCode,po.cCusCode,po.csTCode,po.cexch_name, po.iExchRate,po.cBusType, a.bGsp,a.iTaxRate, a.iUnitPrice
,po.dDate, a.isosid,a.idlsid  ,po.csocode,po.dlid
FROM   dbo.DispatchLists a  
   INNER JOIN dbo.DispatchList po ON a.dlid=po.dlid 
  INNER JOIN dbo.Inventory b  ON a.cInvCode=b.cInvCode
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON a.cUnitID=c.cComunitCode
  WHERE a.idlsid  = '{0}' ", cdlsId);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行发货单ID不正确，查询不到发货单", (i + 1).ToString());
                }
                else
                {
                    if (DbHelper.GetDbString(dt.Rows[0]["cInvCode"]) != rdrecord.bodies[i].cinvcode)
                    {
                        return string.Format("第{0}行订单物料和{1}不一致", (i + 1).ToString(), DbHelper.GetDbString(dt.Rows[0]["cInvCode"]));
                    }
                    //写表头信息
                    if (i == 0)
                    {
                        rdrecord.head.cstcode = DbHelper.GetDbString(dt.Rows[0]["cSTCode"]);
                        rdrecord.head.ccuscode = DbHelper.GetDbString(dt.Rows[0]["cCusCode"]);
                        rdrecord.head.cdepcode = DbHelper.GetDbString(dt.Rows[0]["cDepCode"]);
                        rdrecord.head.cpersoncode = DbHelper.GetDbString(dt.Rows[0]["cPersonCode"]);
                        rdrecord.head.cordercode = DbHelper.GetDbString(dt.Rows[0]["csocode"]);
                        rdrecord.head.cdlcode = DbHelper.GetDbString(dt.Rows[0]["dlid"]); 
                        rdrecord.head.cbustype = DbHelper.GetDbString(dt.Rows[0]["cBusType"]); 
                        //rdrecord01.head.cchkcode = DbHelper.GetDbString(dt.Rows[0]["CCHECKCODE"]);

                    }
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);
                  
                    
                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }


        public static string GetST11Info(Rdrecord11 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {

                
                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu 
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                }
                else
                { 
                    
                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }

        public static string GetST08Info(Rdrecord08 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu 
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }

        public static string GetST09Info(Rdrecord09 rdrecord, string cConstr)
        {
            if (rdrecord == null)
            {
                return "Error: 没有数据";
            }
            if (rdrecord.bodies == null)
            {
                return "Error: 没有表体数据";
            }

            for (int i = 0; i < rdrecord.bodies.Count; i++)
            {


                string cInvCode = rdrecord.bodies[i].cinvcode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,
b.cSTComUnitCode AS cUnitID,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu 
FROM   dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cinvcode  = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行存货编码ID不正确", (i + 1).ToString());
                }
                else
                {

                    rdrecord.bodies[i].cinvcode = DbHelper.GetDbString(dt.Rows[0]["cInvCode"]);


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"])))
                    {
                        rdrecord.bodies[i].cassunit = DbHelper.GetDbString(dt.Rows[0]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iChangRate"])))
                    {
                        rdrecord.bodies[i].iinvexchrate = DbHelper.GetDbString(dt.Rows[0]["iChangRate"]);

                    }
                    if (!string.IsNullOrEmpty(rdrecord.bodies[i].cbatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            rdrecord.bodies[i].cmassunit = DbHelper.GetDbString(dt.Rows[0]["cMassUnit"]);
                            rdrecord.bodies[i].imassdate = DbHelper.GetDbString(dt.Rows[0]["iMassDate"]);
                            if (string.IsNullOrEmpty(rdrecord.bodies[i].dmadedate))
                            {
                                return string.Format("第{0}行订单物料生产日期未录入", (i + 1).ToString());
                            }
                            DateTime dMadedate = DbHelper.GetDbDate(rdrecord.bodies[i].dmadedate);
                            //1 年 2 月 日
                            if (rdrecord.bodies[i].cmassunit == "1")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddYears(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "2")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddMonths(1).ToString("yyyy-MM-dd");
                            }
                            else if (rdrecord.bodies[i].cmassunit == "3")
                            {
                                rdrecord.bodies[i].dvdate = dMadedate.AddDays(1).ToString("yyyy-MM-dd");
                            }


                        }
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"])))
                        {
                            rdrecord.bodies[i].iexpiratdatecalcu = DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]);
                            if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "1")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                            else if (DbHelper.GetDbString(dt.Rows[0]["iExpiratDateCalcu"]) == "2")
                            {
                                rdrecord.bodies[i].dexpirationdate = DbHelper.GetDbDate(rdrecord.bodies[i].dvdate).AddDays(-1).ToString("yyyy-MM-dd");
                            }
                        }

                    }

                }
            }
            return "ok";
        }


        /// <summary>
        /// 货位调整 填写其他信息
        /// </summary>
        /// <param name="rdrecord10"></param>
        /// <param name="cConstr"></param>
        /// <returns></returns>
        public static string GetHWTZInfo(AdjustPVouch entity, string cConstr)
        {
            if (entity == null)
            {
                return "Error: 没有数据";
            }
            if (entity.bodies == null)
            {
                return "Error: 没有表体数据";
            }
            string cposcode = entity.bodies[0].cBPosCode;
            string sqlwh = string.Format(@"SELECT cWhCode,bposend FROM dbo.Position WHERE cPosCode='{0}'   ", cposcode);
            DataTable dtwh = DbHelper2.ExecuteTable(sqlwh, cConstr);
            if (dtwh.Rows.Count == 0)
            {
                return "Error: 移出货位编码不存在";
            }
            else
            {
                int iEnd = DbHelper.GetDbInt(dtwh.Rows[0]["bposend"]);
                if (iEnd == 0)
                {
                    return "Error: 移出货位编码不是末级";
                }
                else
                {
                    string cWhcode = DbHelper.GetDbString(dtwh.Rows[0]["cWhCode"]);
                    entity.head.cWhCode = cWhcode;
                }
            }

            //移入货位
            cposcode = entity.bodies[0].cAPosCode;
            sqlwh = string.Format(@"SELECT cWhCode,bposend FROM dbo.Position WHERE cPosCode='{0}'   ", cposcode);
            dtwh = DbHelper2.ExecuteTable(sqlwh, cConstr);
            if (dtwh.Rows.Count == 0)
            {
                return "Error: 移入货位编码不存在";
            }
            else
            {
                int iEnd = DbHelper.GetDbInt(dtwh.Rows[0]["bposend"]);
                if (iEnd == 0)
                {
                    return "Error: 移入货位编码不是末级";
                }
                else
                {
                    string cWhcode = DbHelper.GetDbString(dtwh.Rows[0]["cWhCode"]);
                    if (entity.head.cWhCode != cWhcode)
                    {
                        return "Error: 移入移出货位编码不属于同一个仓库";
                    }
                }
            }

            for (int i = 0; i < entity.bodies.Count; i++)
            {


                string cInvCode = entity.bodies[i].cInvCode;
                string sql = string.Format(@"SELECT b.cInvCode,b.cComUnitCode,b.cGroupCode,c.cComunitCode,c.iChangRate,b.cMassUnit,b.iMassDate,d.iExpiratDateCalcu
 
FROM dbo.Inventory b  
  INNER JOIN inventory_sub d ON b.cInvCode=d.cInvSubCode
  LEFT JOIN dbo.ComputationUnit c ON b.cSTComUnitCode=c.cComunitCode
  WHERE b.cInvCode = '{0}' ", cInvCode);
                DataTable dt = fuzhu.DbHelper2.ExecuteTable(sql, cConstr);
                if (dt.Rows.Count == 0)
                {
                    return string.Format("第{0}行物料编码不正确", (i + 1).ToString());
                }
                else
                {


                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[i]["cComUnitCode"])))
                    {
                        entity.bodies[i].cAssUnit = DbHelper.GetDbString(dt.Rows[i]["cComUnitCode"]);

                    }

                    if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[i]["iChangRate"])))
                    {
                        entity.bodies[i].iNum = DbHelper.GetDbString(DbHelper.GetDbdecimal(entity.bodies[i].iQuantity) / DbHelper.GetDbdecimal(dt.Rows[i]["iChangRate"]));
                    }

                    if (!string.IsNullOrEmpty(entity.bodies[i].cBatch))
                    {
                        if (!string.IsNullOrEmpty(DbHelper.GetDbString(dt.Rows[0]["cMassUnit"])))
                        {
                            //取批次信息的保质期信息
                            sql = string.Format(@"SELECT cMassUnit,iMassDate,dMadeDate,dVDate,iExpiratDateCalcu,cExpirationdate,dExpirationdate FROM dbo.InvPositionSum
WHERE cinvcode='{0}' AND cBatch='{1}' AND cPosCode='{2}'", cInvCode, entity.bodies[i].cBatch, entity.bodies[i].cBPosCode);
                            DataTable dtbzq = DbHelper2.ExecuteTable(sql, cConstr);
                            if (dtbzq.Rows.Count == 0)
                            {
                                return string.Format("第{0}行物料移出货位无库存", (i + 1).ToString());
                            }
                            else
                            {
                                entity.bodies[i].dMadeDate = DbHelper.GetDbDate(dtbzq.Rows[0]["dMadeDate"]).ToString("yyyy-MM-dd");
                                entity.bodies[i].cMassUnit = DbHelper.GetDbString(dtbzq.Rows[0]["cMassUnit"]);
                                entity.bodies[i].iMassDate = DbHelper.GetDbString(dtbzq.Rows[0]["iMassDate"]);
                                entity.bodies[i].dDisDate = DbHelper.GetDbDate(dtbzq.Rows[0]["dVDate"]).ToString("yyyy-MM-dd");
                                entity.bodies[i].iExpiratDateCalcu = DbHelper.GetDbString(dtbzq.Rows[0]["iExpiratDateCalcu"]);
                                entity.bodies[i].cExpirationdate = DbHelper.GetDbString(dtbzq.Rows[0]["cExpirationdate"]);
                                if (!string.IsNullOrEmpty(DbHelper.GetDbString(dtbzq.Rows[0]["dExpirationdate"])))
                                {
                                    entity.bodies[i].cExpirationdate = DbHelper.GetDbDate(dtbzq.Rows[0]["dExpirationdate"]).ToString("yyyy-MM-dd");
                                }

                            }

                        }
                    }
                }
            }
            return "ok";
        }

    }
}
